package cn.hairui.blog.controller;

import cn.hairui.blog.configure.WebLog;
import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.entity.Artical;
import cn.hairui.blog.entity.Myinfo;
import cn.hairui.blog.entity.User;
import cn.hairui.blog.service.*;
import cn.hairui.blog.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private MyinfoService myinfoService;
    @Autowired
    private ArticalService articalService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private VisitrecordService visitrecordService;

    @RequestMapping(value = "/index")
    @WebLog(description = "首页")
    public String redirectIndex() {
        return "redirect:/";
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @WebLog(description = "首页")
    public String index(HttpServletRequest request, Model model) {
        // 我的信息
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);

        // 分类信息
        List<Map> typesList = typeService.selectByShowFlag(PubConstant.YES_NO_Y, PubConstant.INDEX_TOP_TYPE_COUNT);
        model.addAttribute("types", typesList);

        // 标签信息
        List<Map> tagList = tagService.selectTags(PubConstant.INDEX_TOP_TAG_COUNT, PubConstant.YES_NO_Y);
        model.addAttribute("tags", tagList);

        // 最新推荐
        List<Artical> newList = articalService.listTop(PubConstant.INDEX_TOP_NEWARTICAL_COUNT, "create_time");
        model.addAttribute("newarticals", newList);

        // 本站信息
        Map webinfo = new HashMap();
        int artical_count = articalService.count();// 文章总数
        int type_count = typeService.count();// 分类总数
        int tag_count = tagService.count();// 标签总数
        int comment_count = messageService.count();
        int visit_count = visitrecordService.count();
        int today_visit_count = visitrecordService.countByDate(DateUtil.getCurrentDate());

        webinfo.put("artical_count", artical_count);
        webinfo.put("type_count", type_count);
        webinfo.put("tag_count", tag_count);
        webinfo.put("comment_count", comment_count);
        webinfo.put("visit_count", visit_count);
        webinfo.put("today_visit_count", today_visit_count);

        model.addAttribute("webinfo", webinfo);

        // 分页信息 文章信息
        Integer pageNum = null;
        String pageNumStr = request.getParameter("pageIndex");
        if (pageNumStr != null) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        int pageSize = PubConstant.INDEX_PAGE_SIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<Map> articalList = new ArrayList<>();

        articalList = articalService.selectByShowFlag(PubConstant.YES_NO_Y);// 默认只展示开放的内容
        PageInfo<Map> pageInfo = new PageInfo<Map>(articalList);
        model.addAttribute("articals", articalList);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageIndex", pageNum);
        model.addAttribute("pageSize", pageSize);
        return "index";
    }

    @RequestMapping(value = "/space", method = RequestMethod.GET)
    @WebLog(description = "个人空间")
    public String space(HttpServletRequest request, Model model) {
        // 我的信息
        User user = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if (user == null) {
            return "redirect:/user/login";
        }
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);
        return "space";
    }

    @RequestMapping(value = "/me", method = RequestMethod.GET)
    @WebLog(description = "关于我")
    public String me(Model model) {
        // 我的信息
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);
        return "me";
    }

    @RequestMapping(value = "/tra", method = RequestMethod.GET)
    @WebLog(description = "传送门")
    public String tra(Model model) {
        // 我的信息
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);
        return "tra";
    }

    @RequestMapping(value = "/mikutap")
    @WebLog(description = "音乐节奏")
    public String mikutap(Model model) {
        // 我的信息
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);
        return "mikutap";
    }

    @RequestMapping(value = "/profess")
    @WebLog(description = "表白墙")
    public String profess(HttpServletRequest request, Model model) {
        // 我的信息
        User user = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if (user != null && PubConstant.USER_TYPE_SUPER_ADMIN.equals(user.getType())) {
            Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
            model.addAttribute("myinfo", myinfo);
            return "profess";
        } else {
            return "redirect:error/403";
        }

    }

    @RequestMapping(value = "/cube")
    @WebLog(description = "魔方")
    public String cube(Model model) {
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);
        return "cube";
    }

    @RequestMapping(value = "/pwdgenerator")
    @WebLog(description = "密码生成器")
    public String pwdgenerator(Model model) {
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);
        return "pwdgenerator";
    }

    @RequestMapping(value = "/resume")
    @WebLog(description = "个人简历")
    public String resume(Model model) {
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);
        return "resume";
    }

}
